package com.moon.robot.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.forte.qqrobot.BotRuntime;
import com.forte.qqrobot.beans.messages.result.GroupMemberInfo;
import com.forte.qqrobot.beans.messages.result.LoginQQInfo;
import com.forte.qqrobot.bot.BotManager;
import com.forte.qqrobot.bot.BotSender;
import com.forte.qqrobot.sender.MsgSender;
import com.moon.robot.pojo.GroupMsgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Auther: liaoshiyao
 * @Date: 2019/1/11 11:48
 * @Description: websocket 服务类
 */

/**
 * @ServerEndpoint 这个注解有什么作用？
 * <p>
 * 这个注解用于标识作用在类上，它的主要功能是把当前类标识成一个WebSocket的服务端
 * 注解的值用户客户端连接访问的URL地址
 */


@Component
@ServerEndpoint("/websocket/{name}")
public class WebSocket {


    /**
     * 与某个客户端的连接对话，需要通过它来给客户端发送消息
     */
    private Session session;

    /**
     * 标识当前连接客户端的用户名
     */
    private String name;

    /**
     * 用于存所有的连接服务的客户端，这个对象存储是安全的
     */
    public static ConcurrentHashMap<String, WebSocket> webSocketSet = new ConcurrentHashMap<>();


    @OnOpen
    public void OnOpen(Session session, @PathParam(value = "name") String name) {
        this.session = session;
        this.name = name;
        // name是用来表示唯一客户端，如果需要指定发送，需要指定发送通过name来区分
        webSocketSet.put(name, this);
        System.out.println("[WebSocket] 连接成功，当前连接人数为：=" + webSocketSet.size());
    }


    @OnClose
    public void OnClose() {
        webSocketSet.remove(this.name);
        System.out.println("[WebSocket] 退出成功，当前连接人数为：=" + webSocketSet.size());

    }

    @OnMessage
    public void OnMessage(String message) {
        System.out.println("[WebSocket] 收到消息：" + message);
        JSONObject jsonObject = JSON.parseObject(message);

        BotRuntime runtime = BotRuntime.getRuntime();
        BotManager botManager = runtime.getBotManager();
        BotSender sender = botManager.defaultBot().getSender();
        if ("t10010".equals(jsonObject.get("type"))) {
            String type = "t10010";
            String string = JSON.toJSONString(type);
            GroupSending(string);
            return;
        }
        if ("0".equals(jsonObject.get("type"))) {
            if (jsonObject.getString("msg").equals("")) {
                return;
            }
            String s = "";
            s = sender.SENDER.sendGroupMsg(jsonObject.getString("groupcode"), jsonObject.getString("msg"));
            LoginQQInfo loginQQInfo = sender.GETTER.getLoginQQInfo();
            GroupMemberInfo groupcode = sender.GETTER.getGroupMemberInfo(jsonObject.getString("groupcode"), loginQQInfo.getQQ());
            GroupMsgs groupMsgs = new GroupMsgs(jsonObject.getString("groupcode"), groupcode.getQQCode(), groupcode.getPowerType().TO_STRING, s, jsonObject.getString("msg"), groupcode.getNickname(), groupcode.getQQHeadUrl(), groupcode.getRemark());
            com.alibaba.fastjson.JSONObject o = (com.alibaba.fastjson.JSONObject) JSON.toJSON(groupMsgs);
            o.put("type", "0");
            String string = JSON.toJSONString(o);
            WebSocket.GroupSending(string);
        }

        //判断是否需要指定发送，具体规则自定义
        /*if(message.indexOf("TOUSER") == 0){
            String name = message.substring(message.indexOf("TOUSER")+6,message.indexOf(";"));
            AppointSending(name,message.substring(message.indexOf(";")+1,message.length()));
        }else{
            GroupSending(message);
        }*/

    }

    /**
     * 群发
     *
     * @param message
     */
    public static void GroupSending(String message) {
        if (webSocketSet.size() > 0) {
            for (String name : webSocketSet.keySet()) {
                try {
                    webSocketSet.get(name).session.getBasicRemote().sendText(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 指定发送
     *
     * @param name
     * @param message
     */
    public static void AppointSending(String name, String message) {
        try {
            webSocketSet.get(name).session.getBasicRemote().sendText(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}